package com.tledu.equipment.service.impl;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.equipment.domain.EquipmentInfo;
import com.tledu.equipment.domain.dto.TestResultDTO;
import com.tledu.equipment.mapper.EquipmentInfoMapper;
import com.tledu.equipment.mapper.EquipmentUseMapper;
import com.tledu.equipment.service.IEquipmentInfoService;
import com.tledu.store.mapper.StoreHouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 参试设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-29
 */
@Service
public class EquipmentInfoServiceImpl implements IEquipmentInfoService 
{
    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;
    @Autowired
    private StoreHouseMapper storeHouseMapper;
    @Autowired
    private EquipmentUseMapper equipmentUseMapper;
    /**
     * 查询参试设备
     * 
     * @param id 参试设备主键
     * @return 参试设备
     */
    @Override
    public EquipmentInfo selectEquipmentInfoById(Long id)
    {
        return equipmentInfoMapper.selectEquipmentInfoById(id);
    }

    /**
     * 查询参试设备列表
     * 
     * @param equipmentInfo 参试设备
     * @return 参试设备
     */
    @Override
    public List<EquipmentInfo> selectEquipmentInfoList(EquipmentInfo equipmentInfo)
    {
        return equipmentInfoMapper.selectEquipmentInfoList(equipmentInfo);
    }

    /**
     * 新增参试设备
     * 
     * @param equipmentInfo 参试设备
     * @return 结果
     */
    @Override
    public int insertEquipmentInfo(EquipmentInfo equipmentInfo)
    {
        equipmentInfo.setCreateTime(DateUtils.getNowDate());
        equipmentInfo.setUpdateTime(DateUtils.getNowDate());
        equipmentInfo.setCreateUser(SecurityUtils.getUserId());
        //更新库房实际容量加一
        storeHouseMapper.updateStoreHouse1(equipmentInfo.getStoreId());
        return equipmentInfoMapper.insertEquipmentInfo(equipmentInfo);

    }

    /**
     * 修改参试设备
     * 
     * @param equipmentInfo 参试设备
     * @return 结果
     */
    @Override
    public int updateEquipmentInfo(EquipmentInfo equipmentInfo)
    {
        return equipmentInfoMapper.updateEquipmentInfo(equipmentInfo);
    }

    /**
     * 批量删除参试设备
     * 
     * @param ids 需要删除的参试设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentInfoByIds(Long[] ids)
    {
        return equipmentInfoMapper.deleteEquipmentInfoByIds(ids);
    }

    @Override
    public int applyForTest(Long id) {
        return equipmentInfoMapper.updateEquipmentStatus(id);
    }



    //实验结果按钮逻辑
    public int resultOfTest(TestResultDTO testResultDTO) {
        return equipmentInfoMapper.updateEquipmentStatus1(testResultDTO);
    }

    //出库按钮逻辑
    public int outOfStore(Long id) {

        return equipmentInfoMapper.updateUser_status(id);
    }
    //入库按钮逻辑
    public int putInStore(Long id) {

        return equipmentInfoMapper.putInStore(id);
    }
//    @Override
//    public int logicDeleteEeupmentInfoByIds(Long id) {
//        return equipmentInfoMapper.logicDeleteEquipmentInfoById(id);
//    }

    //逻辑批量删除
    @Override
    public int logicDeleteEeupmentInfoByIds(Long[] ids) {
            //批量删除设备的时候也要减少库房的实际容量
        List<Long> list = new ArrayList();
        for (Long id : ids) {
            list.add(id);
        }
            storeHouseMapper.updateStoreHouse2(list);

        return equipmentInfoMapper.logicDeleteEquipmentInfoByIds(ids);
    }

    /**
     * 删除参试设备信息
     * 
     * @param id 参试设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentInfoById(Long id)
    {
        return equipmentInfoMapper.deleteEquipmentInfoById(id);
    }
}
